#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
uint16_t data;
extern uint8_t Serial_Get_Data_Flag;
void Serial_Init(void)
{
   //配置外设时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
   
  //配置IO口
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//TX复用推挽输出
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOA,&GPIO_InitStructure);
	
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//RX上拉输入
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOA,&GPIO_InitStructure);
	
  //配置串口
  USART_InitTypeDef USART_InitStructure;
  USART_InitStructure.USART_BaudRate=9600;//波特率
  USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//不使用流控
  USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;//发送接收模式
  USART_InitStructure.USART_Parity=USART_Parity_No;//不进行数据校验
  USART_InitStructure.USART_StopBits=USART_StopBits_1;//一位停止位
  USART_InitStructure.USART_WordLength=USART_WordLength_8b;//8位字长
  USART_Init(USART1,&USART_InitStructure);
  
  //配置串口中断
  USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//使能串口接收中断
  
  //配置中断向量控制器
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//配置中断向量组
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  NVIC_Init(&NVIC_InitStructure);

  USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//使能串口申请DMA
  USART_Cmd(USART1,ENABLE);//使能USART
}

void Serial_SendData(uint8_t Byte)
{
  USART_SendData(USART1,Byte);//传送数据给TDR寄存器
  while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET)//等待数据被转移至移位寄存器
	  ;
}

void Serial_SendString(char * string)
{
  for(;*string!='\0';string++)
 {
    Serial_SendData(*string);
 }
}

void Serial_SendArray(uint8_t * array,uint8_t length)
{
   uint8_t i;
   for(i=0;i<length;i++)
   {
	Serial_SendData(*(array+i));
   }
}

uint32_t Serial_pow(uint16_t x,uint16_t y)
{
   uint32_t result=1;
   for(;y>0;y--)
   {
     result*=x;
   }
   return result;
}

void Serial_SendNumber1(uint32_t Num,uint8_t length)
{
   for(;length>0;length--)
   {
     Serial_SendData(Num/Serial_pow(10,length-1)%10+'0');
   }
}

void Serial_SendNumber2(uint32_t Num)//改良版：自动判断数字长度
{
   uint8_t length=0;
   if( Num==0 )
   {
	   length=1;
   }
   while( Num/10>=10 )
   {
      length++;	   
   }
   for(;length>0;length--)
   {
     Serial_SendData(Num/Serial_pow(10,length-1)%10+'0');
   }
}

int fputc(int ch, FILE *f)//printf重定向函数
{
	Serial_SendData(ch);
	return ch;
}

void printf1(char*format,...)//USART1打印函数
{
  char string[200];	
  va_list ap;
  va_start(ap,format);
  vsprintf(string,format,ap);
  Serial_SendString(string);
  va_end(ap);
}

void USART1_IRQHandler(void)
{
  if( USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET )
  {
    data=USART_ReceiveData(USART1);//接收串口数据会自动清除标志位，无需手动清除
	Serial_Get_Data_Flag =1;
  }

}

uint16_t Get_Usart_Rx_Data()
{
   return data;
}








